package com.mxf.code.util;


import com.mxf.code.enums.RedisEnum;
import com.mxf.code.response.LocationListResponse;
import com.mxf.code.response.LocationListWithTreeResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SpaceRedis {

    @Autowired
    private RedisUtils redisUtils;

    // =============================tree============================

    public List<LocationListWithTreeResponse> getLocationTreeList() {
        String key = RedisEnum.SPS_SPACE_TREE.getKey();
        return (List<LocationListWithTreeResponse>) redisUtils.get(key);
    }

    public void saveLocationTreeList(List<LocationListWithTreeResponse> list) {
        String key = RedisEnum.SPS_SPACE_TREE.getKey();
        redisUtils.set(key, list, RedisEnum.SPS_SPACE_TREE.getExpire());
    }

    // ==============================list=============================

    public Map<String, LocationListResponse> getLocationList() {
        String key = RedisEnum.SPS_SPACE_MAP.getKey();
        return (Map) redisUtils.hGetAll(key);
    }


    public void saveLocationMap(Map<String, LocationListResponse> map) {
        String key = RedisEnum.SPS_SPACE_MAP.getKey();
        redisUtils.hMSet(key, new HashMap<>(map), RedisEnum.SPS_SPACE_MAP.getExpire());
        /*redisUtils.hMSet(key, JSON.parseObject(JSON.toJSONString(map),
                        new TypeReference<Map<String, Object>>() {
                        }),
                RedisEnum.SPS_SPACE_MAP.getExpire());*/
    }

    public String getAllLevelSpaceById(Long id) {
        String key = RedisEnum.SPS_SPACE_MAP.getKey();
        LocationListResponse locationListResponse = (LocationListResponse) redisUtils.hGet(key, id.toString());
        if (ObjectUtils.isNotEmpty(locationListResponse)) {
            return locationListResponse.getLabel();
        }
        return id.toString();
    }
}
